<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/26
 * Time: 9:47
 */

namespace app\admin\controller;
use think\Controller;

class Wxx extends Controller
{
    public function wxx(){
        $appid  = 'wx7695c3bffc8c0eb7';
        $backurl = 'http://www.adinhands.com/tengyuan';
        echo $url = $this->wxurl($appid,$backurl);
    }
    function curl_https($url, $data=array(), $header=array(), $timeout=30){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        $response = curl_exec($ch);
        if($error=curl_error($ch)){
            die($error);
        }
        curl_close($ch);
        return $response;
    }
//公众号网页生成授权链接 伪snsapi_base  真 snsapi_userinfo   获取$code
    function wxurl ($appid,$backurl){
        $redirect_uri=urlencode($backurl);
        return $url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
';
    }



//10分钟权限
    function get_access_token($code,$appid,$secret){
        $url ="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$code."&grant_type=authorization_code";
        return json_decode(curl_https($url),true);
    }



//       7天权限刷新access_token
    function get_tokenrefresh($code,$appid,$secret){
        $refresh_token=get_access_token($code,$appid,$secret);
        $raa=$refresh_token['refresh_token'];
//        var_dump($refresh_token);
        return json_decode(curl_https("https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=".$appid."&grant_type=refresh_token&refresh_token=".$raa),true);
    }



//   具体信息
    function get_uersa($code,$appid,$secret){
        $refresh_token=get_tokenrefresh($code,$appid,$secret);
        $access_token=$refresh_token['access_token'];
        $openid=$refresh_token['openid'];
        $url ="https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
        return json_decode(curl_https($url),true);
    }

}